<%-- 
    Document   : search
    Created on : Jul 1, 2011, 9:00:25 AM
    Author     : PhongLK60473
--%>

<%@page import="Object.Page"%>
<%@page import="Object.Order"%>
<%@page import="java.nio.charset.Charset"%>
<%@page import="Object.Book"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%

            String strSearch = request.getParameter("txtSearch");
            String lastSearch;
            
            if(strSearch==null){
                strSearch=Page.getLastSearch(request);
            }
            if(strSearch==null||strSearch.equals("")){
                strSearch = "%";
                lastSearch="";
            } else {
                lastSearch=strSearch;
                strSearch = "%"+strSearch+"%";
            }
            
            Page.setLastSearch(lastSearch, request);

            byte[] utf8 = strSearch.getBytes("UTF-32");
            String ustrSearch = new String(utf8,"UTF-32");

            String[] searchBy = request.getParameterValues("cbSearchBy");
            int intSearchBy;
            if(searchBy!=null&&searchBy.length>0){
                try {
                    intSearchBy = Integer.parseInt(searchBy[0]);
                } catch (NumberFormatException e){
                    intSearchBy=0;
                }
            } else {
                intSearchBy=0;
            }

%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <style type="" >
            @import url("style/style.css");
            @import url("style/search.style.css");
        </style>
    </head>
    <body>
        <%@include file="header.jsp" %>
        <div id="page">

            <% if(Page.isMessage(request)){ %>
            <div class="message"><%= Page.getMessage(request) %></div>
        <% } %>

        <div id="search-pnl">
            <div class="float-left">
                <div class="label" id="label">
                    Search :
                </div>
            </div>
            <div class="float-right">
                <form action="UserController" method="POST">
                    <input type="text" name="txtSearch" id="s" value="<%=lastSearch%>">
                    <select name="cbSearchBy">
                        <option value="0" <%=(intSearchBy==0?"Selected":"")%>>Title</option>
                        <option value="1" <%=(intSearchBy==1?"Selected":"")%>>Author</option>
                        <option value="2" <%=(intSearchBy==2?"Selected":"")%>>Type</option>
                    </select>
                    <input type="submit" name="action" value="Search"/>
                </form>
                    <br>
                (leave blank mean view all)
            </div>
             
        </div>
        <br>
        <%
            
            Book[] result = Book.searchBook(ustrSearch, intSearchBy);
            
        %>
        <div id="search-result">
            <ul>
                <% for(int i=0;i<result.length;i++){%>
                <li>
                    <table border="0px">
                        <tr>
                            <td colspan="2" class="title"><%= result[i].getBookTitle() %></td>
                        </tr>
                        <tr>
                            <td colspan="2" class="description"><%= result[i].getDescription() %></td>
                        </tr>
                        <tr>
                            <td class="author"><%= result[i].getBookAuthor() %></td>
                            <td class="type"><%= result[i].getBookType() %></td>
                        </tr>
                        <tr>
                            <% if(!user.isGuest()){ %>
                            <td colspan="2">
                                <% 
                                int ordered = Order.isOrder(result[i].getBookId(), request);
                                if(ordered>0){
                                    if(ordered==1){
                                    %>
                                        You already have this book in your <a href="UserController?action=ViewCart">cart</a>
                                    <%
                                    }else{
                                %>
                                You have <%=ordered%> in your <a href="UserController?action=ViewCart">cart</a>
                                <%
                                    }
                                }else{
                                %>
                                        Are you have this ? Order Now !
                                <%
                                }
                                %>
                                <div class="order-img">
                                    <a href="UserController?action=Order&bookid=<%= result[i].getBookId()%>">
                                        <img src="images/order-button.png" class=""/></a>
                                </div>
                            </td>
                            <%} else { %>
                            <td colspan="2">
                               Please
                              <a href="UserController?action=Login">
                                Login
                              </a>
                            to order <span class="title"><%= result[i].getBookTitle() %></span>
                            </td>
                            <%} %>
                        </tr>
                    </table>
                </li>
                <%}%>
            </ul>
        </div>
        </div>
        <script>
            document.onsubmit=function(){
                return true;
                if(document.getElementById("s").value.trim()==""){
                    document.getElementById("label").innerHTML="Enter here :";
                    
                    document.getElementById("s").focus();
                    setTimeout(function(){
                        document.getElementById("label").innerHTML="Search :";
                        document.getElementById("label").className="label";
                    },500)
                    return false;
                }
                return true;
            }
        </script>
    </body>
</html>
